#!/usr/bin/perl -w
use strict;
use warnings;
use diagnostics;
use File::Copy;
use feature qw(switch say);
# 
# Program to insert comments
#

sub comment($) {
    my($file) = @_;
    print "Comments File: $file\n";
    if (not ($file =~ m/.*(cpp|c)$/)) {
        print "Not c or cpp!\n";
        return;
    }
    my $tar = $file . ".old";
    copy($file, $tar);
    open(INFO, "< $file")
        or die "Couldn't open $file for reading: $1";
    my $TERM = $/;
    undef $/;
    my $lines = <INFO>;
    $/ = $TERM;
    close(INFO);

    my @tokens = split(m/\n{\n/, $lines);
    my $num = @tokens;
    print "total = $num\n";
    my $fs="";
    my $token;
    foreach $token (@tokens) {
        $fs .= $token;
        if(--$num<=0) {
            next;
        }
        $fs .= "\n{\n";
        if( $token =~ m/.*\)\s*$/s) {
            $fs .= "    ENTER_FUNC;\n";
        }
    }

    open (OUT, "> $file");
    print OUT $fs;
    close (OUT);
}

foreach (@ARGV) {
    comment("$_");
}
1;
